package com.qf.xiaomi.web.servlet;

import com.qf.xiaomi.pojo.User;
import com.qf.xiaomi.service.impl.AddressServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Created by 马英超 on 2022/8/17 8:04
 * @author： 马英超
 * @version：v1.0
 * @description:
 */
public class AddressServlet {
    //地址展示
    protected void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


    }

    /**
     * 添加地址
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    //添加地址
    protected String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        final String uid = req.getParameter("uid");
        final String aname = req.getParameter("aname");
        final String aphone = req.getParameter("aphone");
        final String adetail = req.getParameter("adetail");
        //参数处理
        int id = Integer.parseInt(uid);
        String name = new String(aname.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        String phone = new String(aphone.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        String detail = new String(adetail.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        //参数检查
        if (id == 0 || name == null || name.length() == 0 || phone == null || phone.length() == 0 || detail == null || detail.length() == 0) {
            return "redirect:/error/error.jsp";
        }
        //调用AddressService添加地址
        AddressServiceImpl addressService = new AddressServiceImpl();
        final int addResult = addressService.addAddress(id, name, phone, detail);
        if (addResult > 0)
            return "redirect:/self_info.jsp";
        return "redirect:/error/error.jsp";
    }

    /**
     * 删除地址
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        final String aid = req.getParameter("aid");
        final User user = (User) req.getSession().getAttribute("user");
        //参数处理
        int id = Integer.parseInt(aid);
        //参数检查
        if (id == 0) {
            return "redirect:/error/error.jsp";
        }
        //调用AddressService删除地址
        AddressServiceImpl addressService = new AddressServiceImpl();
        final int deleteResult = addressService.deleteAddress(id, user.getId());
        if (deleteResult > 0)
            return "redirect:/self_info.jsp";
        return "redirect:/error/error.jsp";

    }

    /**
     * 默认地址设置
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */

    protected String setDefault(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //默认地址只能存在一个，因此需要先看表中是否存在默认地址，如果存在则将原来的默认地址值设置为0，然后再按照参数传过来的值设置对于地址为默认地址
            //获取参数
        final String aid = req.getParameter("aid");
        final User user = (User) req.getSession().getAttribute("user");
        //参数处理
        int id = Integer.parseInt(aid);
        final int userId = user.getId();
        //参数检查
        if (id == 0||userId==0) {
            return "redirect:/error/error.jsp";
        }
        //调用AddressService删除地址
        AddressServiceImpl addressService = new AddressServiceImpl();
        final int setResult = addressService.setDefaultAddress(id, userId);
        if (setResult > 0)
            return "redirect:/self_info.jsp";
        return "redirect:/error/error.jsp";


    }

    /**
     * 更新地址
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final String aid = req.getParameter("aid");
        final User user = (User) req.getSession().getAttribute("user");
        final String aname = req.getParameter("aname");
        final String alevel = req.getParameter("alevel");
        final String aphone = req.getParameter("aphone");
        final String adetail = req.getParameter("adetail");

        final int userId = user.getId();
        //参数处理
        int id = Integer.parseInt(aid);
        //参数处理
        String name = new String(aname.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        String phone = new String(aphone.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        String detail = new String(adetail.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        //参数检查
        if (id == 0) {
            return "redirect:/error/error.jsp";
        }
        if (userId==0) {
            return "redirect:/error/error.jsp";
        }
        //调用AddressService删除地址
        AddressServiceImpl addressService = new AddressServiceImpl();
       final int updateResult= addressService.updateAddress(id,name,phone,detail,userId);
       if (updateResult>0)
           return "redirect:/self_info.jsp";
        return "redirect:/error/error.jsp";
    }
}